package com.spider;

import com.spider.entity.VideoInfo;
import com.spider.mapper.BilibiliRankMapper;
import com.spider.util.BUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Created by Lisy on 2018/10/8.
 */
@Service
public class BilibiliRankSpider {

    @Resource
    private BilibiliRankMapper bilibiliRankMapper;


    public void fetchRank() throws IOException {
        int[] index = {0, 1, 168, 3, 129, 4, 36, 160, 119, 155, 5, 181};

        for(int i=0;i<index.length;i++){
            fetchRank(String.format("https://www.bilibili.com/ranking/origin/%d/0/1",index[i]));
        }
    }

    public void fetchRank(String url) throws IOException {
        Document doc = Jsoup.connect(url).get();
        Elements elements = doc.select("#app > div.b-page-body > div > div.rank-container > div.rank-body > div.rank-list-wrap > ul > li");

        String category = doc.select("#app > div.b-page-body > div > div.rank-container > div.rank-body > div.rank-tab-wrap > ul > li.active").text();

        for (int i = 0 ; i < elements.size() ; i++){
            Element ele = elements.get(i);

            Element content = ele.select("div.content").get(0);

            Integer rank = Integer.parseInt(ele.select("div.num").text());

            VideoInfo videoInfo = new VideoInfo();

            videoInfo.title = content.select("div.info > a").text();
            videoInfo.rank = rank;
            videoInfo.category = category;
            videoInfo.url = content.select("div.info > a").attr("href");
            videoInfo.view = BUtil.getCount(content.select("div.info > div.detail > span").text());
            videoInfo.author = content.select("div.info > div.detail > a > span").text();
            videoInfo.score = BUtil.getCount(content.select("div.info > div.pts > div").text());
            videoInfo.avCode = BUtil.getAVCode(videoInfo.url);

            System.out.println(videoInfo);
            System.out.println("==");
            bilibiliRankMapper.insertIntoBilibiliDailyRank(videoInfo);
        }

    }

}
